package com.study.async.controller;

import com.study.async.anno.Loggable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @author Outsider
 * @version 1.0
 * @date 2019/10/27 21:54
 **/
@RestController
@Slf4j
public class AsyncController {

    @GetMapping("/sync")
    @Loggable
    public Object sync() {
        return difficultyWork();
    }

    @GetMapping("/async")
    @Loggable
    public Object async() {
        return (Callable<Object>) this::difficultyWork;
    }

    private Object difficultyWork() {
        log.info("T:{},work started", Thread.currentThread().getName());
        try {
            TimeUnit.MILLISECONDS.sleep((long) (Math.random() * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("T:{},work finished", Thread.currentThread().getName());
        return "done";
    }


}
